package com.lanyuan.thrid.wechat.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * XML 数据接收对象转换工具类
 * 
 */
public class XMLConverUtil {

	/**
	 * map to XML
	 * 
	 * @param object
	 * @return
	 */
	public static String convertToXML(Map<String, String> map) {
		if (MapUtils.isEmpty(map)) {
			return null;
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<xml>");
		for (String key : map.keySet()) {
			sb.append("<").append(key).append(">").append("<![CDATA[").append(map.get(key)).append("]]></").append(key)
					.append(">");
		}
		sb.append("</xml>");
		return sb.toString();
	}

	/**
	 * XML to map
	 * 
	 * @param <T>
	 * @param clazz
	 * @param reader
	 * @return
	 */
	public static Map<String, String> convertToMap(String xmlStr) {
		try {
			Map<String, String> map = new HashMap<String, String>();
			Document document = XMLUtil.getDocument(xmlStr.replace("\n", ""));
			Node root = document.getElementsByTagName("xml").item(0);
			NodeList nodeList = root.getChildNodes();
			for (int i = 0; i < nodeList.getLength(); i++) {
				Node node = nodeList.item(i);
				map.put(node.getNodeName(), node.getFirstChild().getNodeValue());
			}
			return map;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * XML to map
	 * 
	 * @param <T>
	 * @param clazz
	 * @param reader
	 * @return
	 */
	public static Map<String, String> convertToMap(InputStream xmlIs) {
		BufferedReader br = new BufferedReader(new InputStreamReader(xmlIs));
		String line = null;
		StringBuilder sb = new StringBuilder();
		try {
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return convertToMap(sb.toString());
	}

	public static void main(String[] args) {
		String str = "<xml><return_code><![CDATA[SUCCESS]]></return_code>" + "<return_msg><![CDATA[OK]]></return_msg>"
				+ "<appid><![CDATA[wxc98a2cacffb4eb81]]></appid>" + "<mch_id><![CDATA[1293499201]]></mch_id>"
				+ "<nonce_str><![CDATA[McdeO7Op7LsEX0Po]]></nonce_str>"
				+ "<sign><![CDATA[254D14B72677FD74F3A04195A69F2354]]></sign>"
				+ "<result_code><![CDATA[SUCCESS]]></result_code>"
				+ "<prepay_id><![CDATA[wx20160225100855a28ae661680871387361]]></prepay_id>"
				+ "<trade_type><![CDATA[JSAPI]]></trade_type>" + "</xml>";
		Document document = XMLUtil.getDocument(str);
		Node root = document.getElementsByTagName("xml").item(0);
		NodeList nodeList = root.getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			Node node = nodeList.item(i);
			System.out.println(node.getNodeName() + "=" + node.getFirstChild().getNodeValue());
		}
	}
}
